<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 将驼峰命名转换成小写，中间加分隔符 例：AuthRole -> auth_role
 * @param string $str 要转换的字符串
 * @param string $separator 分隔符
 * @return string $str 
 */
function unhump($str, $separator = '_') {
	//查找小写和大写字母紧挨着的地方，添加分隔符，最后转换成小写
	$str = strtolower(preg_replace('/([a-z])([A-Z])/', '$1'.$separator.'$2', $str));
	return $str;
}

/**
 * 将数据格式化成树形结构
 * @param array $data
 * @return array
 */
function getTree($data, $pid = 0)
{
	$tree = '';
	foreach($data as $k => $v)
	{
	  if($v['pid'] == $pid)
	  {
	   $v['son'] = getTree($data, $v['id']);
	   $tree[] = $v;
	  }
	}
	return $tree;
}

/**
 * 检查权限
 * @param array $name 	$name = controller/action
 * @param array $uid 	认证用户的id 
 * @return boolean 		通过验证返回true;失败返回false
 */
 function checkPermissions($name, $uid) {
 	$auth = new \app\index\controller\Auth();
 	//判断是否是超级管理员
 	$group = $auth->getGroups($uid);
 	if($group[0]['group_id'] == 1) {
 		return true;
 	} else {
 		return $auth->check($name, $uid);
 	}
 }
 	
/**
 *将树形结构数据转换成HTML输出
 *@param array $tree
 *@return array
 */
function htmlTree($tree, $rules = array()) {
	$html = '';
	foreach ($tree as $v) {
		if ($v['son'] == '') {
			if ($v['pid'] == 0) {
				if (in_array($v['id'],$rules)) {
					$html .= '<ul class="dn-row-ckx" style="margin-left:18px;">
			                    <li class="dn-level-li">
				                <span class="dn-ckx-icon"></span>
				                <label>
				                    <span class="ui-ckx"></span>
				                    <input type="checkbox" class="ace" name="rules[]" checked value="'.$v['id'].'" /> <span class="lbl">'.$v['title'].'</span>
			                        </label>
			                    </li>
			                </ul>';
			    } else {
			    	$html .= '<ul class="dn-row-ckx" style="margin-left:18px;">
			                    <li class="dn-level-li">
				                <span class="dn-ckx-icon"></span>
				                <label>
				                    <span class="ui-ckx"></span>
				                    <input type="checkbox" class="ace" name="rules[]" value="'.$v['id'].'" /> <span class="lbl">'.$v['title'].'</span>
			                        </label>
			                    </li>
			                </ul>';
			    }
			} else {
				if (in_array($v['id'],$rules)) {
					$html .= '<ul class="dn-row-ckx" style="display:none">
			                    <li class="dn-level-li">
				                <span class="dn-ckx-icon"></span>
				                <label>
				                    <span class="ui-ckx"></span>
				                    <input type="checkbox" class="ace" name="rules[]" checked value="'.$v['id'].'" /> <span class="lbl">'.$v['title'].'</span>
			                        </label>
			                    </li>
			                </ul>';
			    } else {
			    	$html .= '<ul class="dn-row-ckx" style="display:none">
			                    <li class="dn-level-li">
				                <span class="dn-ckx-icon"></span>
				                <label>
				                    <span class="ui-ckx"></span>
				                    <input type="checkbox" class="ace" name="rules[]" value="'.$v['id'].'" /> <span class="lbl">'.$v['title'].'</span>
			                        </label>
			                    </li>
			                </ul>';
			    }
		    }

		} else {
			if ($v['pid'] == 0) {
				if (in_array($v['id'],$rules)) {
					$html .= '<ul class="dn-row-ckx">
			                    <li class="dn-level-li">
				                <span class="dn-ckx-icon"><i class="fa fa-plus-square" aria-hidden="true" style="font-size:18px;color:#428bca;position:relative;top:2px;cursor:pointer"></i></span>
				                <label>
				                    <span class="ui-ckx"></span>
				                    <input type="checkbox" class="ace" name="rules[]" checked value="'.$v['id'].'" /> <span class="lbl">'.$v['title'].'</span>
			                        </label>';
			    } else {
			    	$html .= '<ul class="dn-row-ckx">
			                    <li class="dn-level-li">
				                <span class="dn-ckx-icon"><i class="fa fa-plus-square" aria-hidden="true" style="font-size:18px;color:#428bca;position:relative;top:2px;cursor:pointer"></i></span>
				                <label>
				                    <span class="ui-ckx"></span>
				                    <input type="checkbox" class="ace" name="rules[]" value="'.$v['id'].'" /> <span class="lbl">'.$v['title'].'</span>
			                        </label>';
			    }
			} else {
				if (in_array($v['id'],$rules)) {
					$html .= '<ul class="dn-row-ckx" style="display:none">
			                    <li class="dn-level-li">
				                <span class="dn-ckx-icon"><i class="fa fa-plus-square" aria-hidden="true" style="font-size:18px;color:#428bca;position:relative;top:2px;cursor:pointer"></i></span>
				                <label>
				                    <span class="ui-ckx"></span>
				                    <input type="checkbox" class="ace" name="rules[]" checked value="'.$v['id'].'" /> <span class="lbl">'.$v['title'].'</span>
			                        </label>';
			    } else {
			    	$html .= '<ul class="dn-row-ckx" style="display:none">
			                    <li class="dn-level-li">
				                <span class="dn-ckx-icon"><i class="fa fa-plus-square" aria-hidden="true" style="font-size:18px;color:#428bca;position:relative;top:2px;cursor:pointer"></i></span>
				                <label>
				                    <span class="ui-ckx"></span>
				                    <input type="checkbox" class="ace" name="rules[]" value="'.$v['id'].'" /> <span class="lbl">'.$v['title'].'</span>
			                        </label>';
			    }
			}
			$html .= htmlTree($v['son'], $rules);
			$html = $html.'				</span>
			                        </label>
			                    </li>
			                </ul>';
		}
	}
	return $html;
}

/**
 * 将数据格式化成树形结构
 * @param int $count 		数据总量
 * @param int $pageCount 	每页显示的条数数
 * @param int $currentPage 	当前页码
 * @param char $table 		数据表
 * @param boolean $pageHtml true:返回分页代码 false:返回分页数据
 * @param string $where 	筛选条件
 * @return true:string 		分页html代码
 * @return false:array 		分页数据
 */
 function page($count, $pageCount, $currentPage, $table, $pageHtml = false, $where = '1=1', $searchstr = null) {
 	//分页总数
 	$countPage = ceil($count/$pageCount);
 	if ($currentPage > $countPage) {
 		$currentPage = $countPage;
 	}
 	if ($currentPage < 1) {
 		$currentPage = 1;
 	}
 	$pageData = \think\Db::name($table)->where($where)->limit(($currentPage-1)*$pageCount,$pageCount)->select();
 	if ($pageHtml == false) {
 		return $pageData;
 	}
 	$pageHtml  = '<ul class="pagination">';
 	if ($currentPage > 1) {
 		if($searchstr == null) {
 			$pageHtml .= '	<li><a href="?page='.($currentPage-1).'">&laquo;</a></li>';
 		} else {
 			$pageHtml .= '	<li><a href="?page='.($currentPage-1).'&'.$searchstr.'">&laquo;</a></li>';
 		}
 		
 	} else {
 		$pageHtml .= '	<li class="disabled"><a href="javascrit:;">&laquo;</a></li>';
 	}
 	for ($i = 1; $i <= $countPage; $i++) {
 		if ($i == $currentPage) {
 			if($searchstr == null) {
 				$pageHtml .= '	<li class="active"><a href="?page='.$i.'">'.$i.'</a></li>';
 			} else {
 				$pageHtml .= '	<li class="active"><a href="?page='.$i.'&'.$searchstr.'">'.$i.'</a></li>';
 			}
 			
 		} else {
 			if($searchstr == null) {
 				$pageHtml .= '	<li><a href="?page='.$i.'">'.$i.'</a></li>';
 			} else {
 				$pageHtml .= '	<li><a href="?page='.$i.'&'.$searchstr.'">'.$i.'</a></li>';
 			}
 		}
 	}
 	if ($currentPage < $countPage) {
 		if($searchstr == null) {
 			$pageHtml .= '	<li><a href="?page='.($currentPage+1).'">&raquo;</a></li>';
 		} else {
 			$pageHtml .= '	<li><a href="?page='.($currentPage+1).'&'.$searchstr.'">&raquo;</a></li>';
 		}
 		
 	} else {
 		$pageHtml .= '	<li class="disabled"><a href="javascrit:;">&raquo;</a></li>';
 	}
 	
 	$pageHtml .= '</ul>';
 	return $pageHtml;	
 }